package com.hc.websocket.sse.controller;

import com.hc.websocket.sse.service.SseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequestMapping("/sse")
public class SseController {

    @Autowired
    private SseService sseService;

    @GetMapping(value = "/connect/{clientId}",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter connect(@PathVariable String clientId) {
        return sseService.createConnect(clientId);
    }

    @GetMapping("/send/{clientId}")
    public void sendMessageToOneClient(@PathVariable String clientId, @RequestParam("msg") String msg) {
        sseService.sendMessageToOneClient(clientId, msg);
    }

    @PostMapping("/broadcast")
    public void sendMessageToAllClient(@RequestBody String msg) {
        sseService.sendMessageToAllClient(msg);
    }

    @GetMapping("/close/{clientId}")
    public void closeConnect(@PathVariable String clientId){
        sseService.closeConnect(clientId);
    }
}